package com.haima.ziliao.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haima.common.utils.StringUtils;
import com.haima.ziliao.domain.PXueke;
import com.haima.ziliao.service.IPXuekeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PNianji;
import com.haima.ziliao.service.IPNianjiService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 年级管理Controller
 * 
 * @author hima
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/ziliao/nianji")
public class PNianjiController extends BaseController
{
    @Autowired
    private IPNianjiService pNianjiService;
    @Autowired
    private IPXuekeService xuekeService;

    /**
     * 查询年级管理列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:nianji:list')")
    @GetMapping("/list")
    public TableDataInfo list(PNianji pNianji)
    {
        startPage();
        List<PNianji> list = pNianjiService.selectPNianjiList(pNianji);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public AjaxResult listAll()
    {
        List<PNianji> list = pNianjiService.list(new QueryWrapper<PNianji>().eq("pid",0));
        return AjaxResult.success(list);
    }

    @GetMapping("/listAllNj")
    public AjaxResult listAllNj()
    {
        List<PNianji> list = pNianjiService.list(new QueryWrapper<PNianji>().ne("pid",0));
        return AjaxResult.success(list);
    }

    @GetMapping("/listAllNjSon")
    public AjaxResult listAllNjSon(String kcs)
    {
        if(StringUtils.isEmpty(kcs)){
            List<PNianji> list = pNianjiService.list(new QueryWrapper<PNianji>().ne("pid",0));
            for(int i=0;i<list.size();i++){
                String xueke = list.get(i).getXks();
                List<String> xkid = new ArrayList<>(Arrays.asList(xueke.split(",")));
                List<PXueke> xkss = xuekeService.list(new QueryWrapper<PXueke>()
                        .in("id",xkid));
                list.get(i).setXueke(xkss);
            }
            return AjaxResult.success(list);
        }else{
            List<PNianji> list = pNianjiService.list(new QueryWrapper<PNianji>().ne("pid",0));
            for(int i=0;i<list.size();i++){
                String xueke = kcs;
                List<String> xkid = new ArrayList<>(Arrays.asList(xueke.split(",")));
                List<PXueke> xkss = xuekeService.list(new QueryWrapper<PXueke>()
                        .in("id",xkid));
                list.get(i).setXueke(xkss);
            }
            return AjaxResult.success(list);
        }
    }

    /**
     * 导出年级管理列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:nianji:export')")
    @Log(title = "年级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PNianji pNianji)
    {
        List<PNianji> list = pNianjiService.selectPNianjiList(pNianji);
        ExcelUtil<PNianji> util = new ExcelUtil<PNianji>(PNianji.class);
        util.exportExcel(response, list, "年级管理数据");
    }

    /**
     * 获取年级管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:nianji:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pNianjiService.selectPNianjiById(id));
    }

    /**
     * 新增年级管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:nianji:add')")
    @Log(title = "年级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PNianji pNianji)
    {
        return toAjax(pNianjiService.insertPNianji(pNianji));
    }

    /**
     * 修改年级管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:nianji:edit')")
    @Log(title = "年级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PNianji pNianji)
    {
        return toAjax(pNianjiService.updatePNianji(pNianji));
    }

    /**
     * 删除年级管理
     */
    @PreAuthorize("@ss.hasPermi('ziliao:nianji:remove')")
    @Log(title = "年级管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pNianjiService.deletePNianjiByIds(ids));
    }
}
